package com.fantasy.blog.controller;


import com.fantasy.blog.common.aop.LogAnnotation;
import com.fantasy.blog.service.ArticleService;
import com.fantasy.blog.vo.Result;
import com.fantasy.blog.vo.params.ArticleParam;
import com.fantasy.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    /**
     * 首页文章列表
     * @param pageParams
     * @return
     */
    @LogAnnotation()
    @PostMapping
    public Result listArticle(@RequestBody PageParams pageParams){

        return articleService.listArticle(pageParams);
    }

    @LogAnnotation()
    @GetMapping("{id}")
    public Result getArticleById(@PathVariable("id") Long id){
        return articleService.findArticleById(id);
    }

    /**
     * 最热文章
     * @return
     */
    @LogAnnotation()
    @GetMapping("hot")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 最新文章
     * @return
     */
    @LogAnnotation()
    @GetMapping("new")
    public Result newArticle(){
        int limit = 3;
        return articleService.newArticle(limit);
    }

    /**
     * 文章归档
     * @return
     */
    @LogAnnotation()
    @GetMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    @LogAnnotation()
    @GetMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    @LogAnnotation()
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }

    @LogAnnotation()
    @PostMapping("del/{id}")
    public Result publish(@PathVariable("id") Long articleId){
        return articleService.delByArticleId(articleId);
    }
}
